package top.lenjon.lib.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.lenjon.lib.entity.Book;
import top.lenjon.lib.entity.Log;
import top.lenjon.lib.mapper.LogMapper;
import top.lenjon.lib.service.ILogService;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 借阅记录 服务实现类
 * </p>
 *
 * @author wei
 * @since 2020-11-09
 */
@Service
@CacheConfig(cacheNames = "bookLog")
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private BookServiceImpl bookService;


    @Override
    public List<Log> getAllLogOf(int borrowerId) {
        return logMapper.getAllLogOf(borrowerId);
    }

    @Override
    public List<Book> getAllBookOf(int borrowerId) {
        List<Book> books = new ArrayList<>();
        logMapper.getAllLogOf(borrowerId).forEach(log -> books.add(bookService.getById(log.getBookId())));
        return books;
    }

    @Override
    public List<Log> getUnReturnLogOf(int borrowerId) {
        return logMapper.getNotReturnLogOf(borrowerId);
    }

    @Override
    public List<Book> getUnReturnBookOf(int borrowerId) {
        List<Book> books = new ArrayList<>();
        logMapper.getNotReturnLogOf(borrowerId).forEach(log -> books.add(bookService.getById(log.getBookId())));
        return books;
    }

    @Override
    public List<Log> getReturnedLogOf(int borrowerId) {
        List<Log> logs = new ArrayList<>();
        logMapper.getAllLogOf(borrowerId).forEach(log -> {
            if (log.getReturnTime() != null) {
                logs.add(log);
            }
        });
        return logs;
    }

    @Override
    public List<Book> getReturnedBookOf(int borrowerId) {
        List<Book> books = new ArrayList<>();
        getReturnedLogOf(borrowerId).forEach(log -> books.add(bookService.getById(log.getBookId())));
        return books;
    }

    @Override
    public List<Log> getLogShouldBeRenewOf(int borrowerId) {
        List<Log> logs = getUnReturnLogOf(borrowerId);
        List<Log> ret = new ArrayList<>();
        logs.forEach(log -> {
            long diff = Duration.between(log.getBorrowTime(), LocalDateTime.now()).toDays() - log.getRenewTimes() * 7;
            if (diff >= 24 && diff <= 30) {
                //30天应归还，一次续借加7天
                //借出天数 - 续借时间*7 小于 30天
                ret.add(log);
            }
        });
        return ret;
    }

    @Override
    public List<Book> getBookShouldBeRenewOf(int borrowerId) {
        List<Book> books = new ArrayList<>();
        getLogShouldBeRenewOf(borrowerId).forEach(log -> books.add(bookService.getById(log.getBookId())));
        return books;
    }

    @Override
    public List<Log> getLogExpiredLogOf(int borrowerId) {
        List<Log> logs = getUnReturnLogOf(borrowerId);
        List<Log> ret = new ArrayList<>();
        logs.forEach(log -> {
            if (Duration.between(log.getBorrowTime(), LocalDateTime.now()).toDays() - log.getRenewTimes() * 7 > 30) {
                //借出天数 - 续借次数*7 应 < 30
                ret.add(log);
            }
        });
        return ret;
    }

    @Override
    @Cacheable(key = "#status+#root.methodName+#pageNum")
    public PageInfo getLogByStatus(int status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        switch (status) {
            case 2:
                return new PageInfo<>(logMapper.getAll());
            case 1:
                return new PageInfo<>(logMapper.getNoRe());
            case 0:
                return new PageInfo<>(logMapper.geths());
            default:
                return null;
        }
    }

    @Override
    public Log getLogById(int id) {
        return logMapper.selectById(id);
    }

    @Override
    public List<Book> getLogExpiredBookOf(int borrowerId) {
        List<Book> books = new ArrayList<>();
        getLogExpiredLogOf(borrowerId).forEach(log -> books.add(bookService.getById(log.getBookId())));
        return books;
    }

    @Override
    public void renew(int bookId) {
        logMapper.renew(bookId);
    }

    @Override
    public List<Log> getLogByUserId(Integer userId) {
        return logMapper.getAllLogOf(userId);
    }

    @Override
    public void addLog(int bookId, int borrowerId) {

    }

}
